package com.gui.settings;

import com.data.Settings;
import com.gooddies.swing.hCheckBox;
import com.gooddies.texteditors.DefaultIntegerTextField;
import com.gooddies.texteditors.DefaultTextField;
import com.gui.mainwindow.MainWindow;
import com.swingson.SwingsonGuiBuilder;
import java.awt.Frame;
import javax.swing.JDialog;

/**
 * @author sad
 */
public class SettingsDialog extends JDialog {

    private final DefaultTextField fbEmailTf = new DefaultTextField();
    private final DefaultTextField fbPasswordTf = new DefaultTextField();
    private final DefaultTextField fbAppIdTf = new DefaultTextField();
    private final DefaultTextField fbAppSecretTf = new DefaultTextField();
    private final DefaultTextField cameraUrlTf = new DefaultTextField();
    private final DefaultTextField cameraUserTf = new DefaultTextField();
    private final DefaultTextField cameraPasswordTf = new DefaultTextField();
    private final DefaultIntegerTextField cameraFrameRateTf = new DefaultIntegerTextField(0, 300);
    private final DefaultTextField mailServerTf = new DefaultTextField();
    private final DefaultTextField mailAddressTf = new DefaultTextField();
    private final DefaultTextField mailPasswordTf = new DefaultTextField();
    private final hCheckBox bottomTopRightLeftUniverseDirectionCB = new hCheckBox();
    private final Settings settings;

    public SettingsDialog(Frame owner, Settings settings) {
        super(owner, true);
        this.settings=settings;
        setTitle("Settings");
        SwingsonGuiBuilder.createGuiFromJsonInPackage(this);
        fbEmailTf.setText(settings.getFaceBookEmail());
        fbPasswordTf.setText(settings.getFaceBookPassword());
        fbAppIdTf.setText(settings.getFacebookAppId());
        fbAppSecretTf.setText(settings.getFacebookAppSecret());
        cameraUrlTf.setText(settings.getCameraUrl());
        cameraUserTf.setText(settings.getCameraUser());
        cameraPasswordTf.setText(settings.getCameraPassword());
        cameraFrameRateTf.setValue(settings.getCameraFrameRate());
        mailServerTf.setText(settings.getMailServer());
        mailAddressTf.setText(settings.getMailAddress());
        mailPasswordTf.setText(settings.getMailPassword());
        bottomTopRightLeftUniverseDirectionCB.setSelected(settings.isBottomUpLeftRightUniverseProcessing());
    }

    private void onSave() {
        settings.setFaceBookEmail(fbEmailTf.getText());
        settings.setFaceBookPassword(fbPasswordTf.getText());
        settings.setFacebookAppId(fbAppIdTf.getText());
        settings.setFacebookAppSecret(fbAppSecretTf.getText());
        settings.setCameraUrl(cameraUrlTf.getText());
        settings.setCameraUser(cameraUserTf.getText());
        settings.setCameraPassword(cameraPasswordTf.getText());
        settings.setCameraFrameRate(cameraFrameRateTf.getValue());
        settings.setMailServer(mailServerTf.getText());
        settings.setMailAddress(mailAddressTf.getText());
        settings.setMailPassword(mailPasswordTf.getText());
        settings.setBottomUpLeftRightUniverseProcessing(bottomTopRightLeftUniverseDirectionCB.isSelected());
        setVisible(false);
    }

    private void onCancel() {
        setVisible(false);
    }
}
